<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport"
        content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="../css/aui.2.0.css" />
    <style type="text/css">
        .class-title {
            margin-top: 10px;
            margin-left: 10px;
            margin-right: 10px;
            font-size: 20px;
            font-weight: bold;
            color: #00BFFF;
        }

        .class-hr {
            margin-top: 5px;
            height: 1px;
            border: none;
            border-top: 1px solid #DCDCDC;
        }

        .class-seqno {
            margin-top: 3px;
            margin-left: 10px;
            margin-right: 25px;
            font-size: 10px;
            color: #808080;
            font-weight: bold;
            text-align: right;
        }

        .class-table {
            width: 100%;
            text-align: left;
            color: black;
            font-size: 13px;
            margin-left: 2%;
            width: 100%;
            table-layout: fixed;
        }

        .class-td {
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
        }

        .lw {
            background-color: #DCE9F2;
            border-top: 1px solid #CBD6DE;
            border-bottom: 1px solid #ADBECB;
            color: #467aa7;
            font-size: 12px;
            font-weight: bold;
        }

        .lw1 {
            background-color: #f0f5f5;
            border-top: 1px solid #CBD6DE;
            border-bottom: 1px solid #ADBECB;
            color: #467aa7;
            font-size: 12px;
        }
        .aui-searchbar-input {
            width: 95% !important;
        }
    </style>
</head>

<body>
    <header id='header' class="aui-bar aui-bar-nav" style="background-color: #03a9f4">
        <a class="aui-pull-left aui-btn" onclick="api.closeWin();">
            <span class="aui-iconfont aui-icon-left"></span>
        </a>
        <div class="aui-title" id="middle">
            东阳计划发货-发货单明细

        </div>
    </header>
    <div class="aui-content aui-margin-b-15">
        <ul class="aui-list aui-form-list">
            <li class="aui-list-item" style="background-color: rgb(187, 185, 185);">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">发货计划号</div>
                    <div class="aui-list-item-input">
                        <input type="text" id="invoiceplancode" value="" disabled="true" />
                    </div>
                </div>
            </li>

            <div class="aui-btn aui-btn-info aui-btn-block"><input type="text" class="aui-input"
                    style="width: 80%;font-size: 16px;" id="message" /></div>
            <div style="background-color:white;width: 100%;margin-top:2%;margin-bottom:5px;">
                <table class="table" style='width:100%;' id="table">
                    <thead>
                        <tr
                            style="text-align: center;background-color: #DCE9F2;color: #467aa7;font-size: 8px;font-weight: bold;">
                            <td algin='center'>物料编码</td>
                            <td algin='center'>规格</td>
                            <td algin='center'>计划数量</td>
                            <td algin='center'>绑定数量</td>
                        </tr>
                    </thead>
                    <tbody id="tbodyHtml">
                    </tbody>
                </table>
            </div>
            
        </ul>

        <ul class="ui-selectee" id="tbodyHtmDYFHD" style="margin-bottom:5px;">
        </ul>

        <div style="width:100%;margin-top:18%;"></div>
    </div>
    <footer class="aui-bar aui-bar-tab" id="footer">
        <div style="width:100%;margin-top:2%;"></div>
        <div class="aui-btn aui-btn-info aui-btn-block" onclick="beforeSave()" id="submitButton">确认发货</div>
    </footer>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="../script/public.js"></script>
<script>
    //光标所在id
    var idSelector = "";

    var invoiceplancode = "";
    var subBussinessType = "";
    var mrlCode = "";
    var invoiceinfoid="";
    var title = "";
    var cardColor = 'white';

    apiready = function () {
        invoiceplancode = api.pageParam.invoiceplancode;
        invoiceinfoid=api.pageParam.invoiceinfoid;
        subBussinessType = api.pageParam.subBussinessType;
        title = api.pageParam.titleText;
        $("#invoiceplancode").val(invoiceplancode);

        // 查询物料信息
        doSerchInvoiceInfo(invoiceplancode)

        if (title != undefined && title != null && title != "") {
            document.getElementById("middle").innerHTML = title;
        }

        //查询东阳发货单
        doSearchDYFHD();

    }

    // 查询物料信息
    function doSerchInvoiceInfo(invoiceplancode) {
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/doSerchInvoiceInfo";
        var data = {
            values: { ////以表单方式提交参数（JSON对象）
                un: un,
                invoiceplancode: invoiceplancode,
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //成功
                if (myobj.code == '0') {
                    var list = eval(myobj.list);
                    var html = "";
                    for (var i = 0; i < list.length; i++) {
                        html += "<tr style='text-align: center;color: black;font-size: 10px;background-color: #F5F5F5;'>";
                        html += "<td name='mrlCode'>" + list[i].mrlCode + "</td>";
                        html += "<td name='drawing'>" + list[i].drawing + "</td>";
                        html += "<td name='planQty'>" + list[i].planQty + "</td>";
                        html += "<td name='bdQty'>" + list[i].bdQty + "</td>";
                        html += "</tr>";
                    }
                    $api.html($api.byId("tbodyHtml"), html);
                } else if (myobj.code == '1') {
                    api.hideProgress();
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            }
        });
    }


    //查询东阳发货单
    function doSearchDYFHD() {

        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/queryDYFHD";
        var data = {
            values: {
                invoiceplancode: invoiceplancode,
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function(ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //返回成功
                if (myobj.code == "0") {
                    var list = eval(myobj.list);
                    var html = "";
                    for (var i = 0; i < list.length; i++) {

                        if(list[i].planQty != list[i].actualQty ){

                            if(list[i].fhType == 'E'){
                                cardColor = 'red';
                            }else {
                                if(list[i].bdQty == 0) {
                                    cardColor = 'white';
                                } else if(list[i].bdQty == list[i].planQty) {
                                    cardColor = 'lightgreen';
                                } else if(list[i].bdQty < list[i].planQty) {
                                    cardColor = 'yellow';
                                }
                            }
                            
                            html += "<li class='sortable ui-selectee ui-sortable'>";
                            html += "<ul class='aui-list aui-media-list ui-selectee'>";
                            html += "<li class='aui-list-item  ui-selectee'>";
                            html += "<div class='aui-media-list-item-inner ui-selectee' style='background-color: " + cardColor + ";'>";
                            html += "<div class='aui-list-item-inner ui-selectee'>";
                            html += "<div class='aui-list-item-text ui-selectee'  style='height: 30px;'>";
                            html += "<div class='aui-list-item-label ui-selectee list-tr-title'>发货单号</div>";
                            html += "<div class='aui-list-item-inner ui-selectee'>" + list[i].code + "</div>";
                            html += "</div>";
                            
                            html += "<div class='aui-list-item-text ui-selectee' style='height: 30px;'>";
                            html += "<div class='aui-list-item-label ui-selectee list-tr-title'>东阳订单号</div>";
                            html += "<div class='aui-list-item-inner ui-selectee'>" + list[i].dyDDH + "</div>";
                            html += "</div>";

                            html += "<div class='aui-list-item-text ui-selectee' style='height: 30px;'>";
                            html += "<div class='aui-list-item-label ui-selectee list-tr-title'>物料编码</div>";
                            html += "<div class='aui-list-item-inner ui-selectee'>" + list[i].mrlCode + "</div>";
                            html += "</div>";

                            html += "<div class='aui-list-item-text ui-selectee' style='height: 30px;'>";
                            html += "<div class='aui-list-item-label ui-selectee list-tr-title'>规格</div>";
                            html += "<div class='aui-list-item-inner ui-selectee'>" + list[i].drawing + "</div>";
                            html += "</div>";

                            html += "<div class='aui-list-item-text ui-selectee' style='height: 30px;'>";
                            html += "<div class='aui-list-item-label ui-selectee list-tr-title'>物料名称</div>";
                            html += "<div class='aui-list-item-inner ui-selectee'>" + list[i].mrlName + "</div>";
                            html += "</div>";

                            html += "<div class='aui-list-item-text ui-selectee'  style='height: 30px;'>";
                            html += "<div class='aui-list-item-label ui-selectee list-tr-title' style='width: 45%;'>计划发货数量</div>";
                            html += "<div class='aui-list-item-inner ui-selectee' style='margin-left: 0.1rem;'>" + list[i].planQty + "</div>";
                            html += "</div>";
                            html += "<div class='aui-list-item-text ui-selectee'  style='height: 30px;'>";
                            html += "<div class='aui-list-item-label ui-selectee list-tr-title' style='width: 45%;'>已绑定数量</div>";
                            html += "<div class='aui-list-item-inner ui-selectee' style='margin-left: 0.1rem;'>" + list[i].bdQty + "</div>";
                            html += "</div>";
                            html += "<div class='aui-list-item-text ui-selectee' style='height: 30px;'>";
                            html += "<div class='aui-list-item-label ui-selectee list-tr-title' style='width: 70%;'>实际发货数量</div>";
                            html += "<div class='aui-list-item-inner ui-selectee' style='margin-left: 0.1rem;'>" + list[i].actualQty + "</div>";

                            html += "<div class='aui-list-item-right aui-btn ui-selectee' " +
                                "onclick='goPageDetail(\"" + list[i].invoiceplancode +"\",\""+ list[i].invoiceplanid+"\",\""+ list[i].code+"\");'>绑定物料</div>";

                            html += "</div>";
                            
                            
                            html += "</div>";
                            html += "</div>";
                            html += "</div>";
                            html += "</li>";
                            html += "</ul>";
                            html += "</li>";
                        }
                    }
                    $api.html($api.byId("tbodyHtmDYFHD"), html);
                    //失败
                } else if (myobj.code == "1") {
                    //清空表格数据
                    $api.html($api.byId("tbodyHtmDYFHD"), "");
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            }
        });
    }

    //跳转详情页面-绑定物料
    function goPageDetail(invoiceplancode,invoiceplanid,fhdhCode) {
        //打开页面
        api.openWin({
            name: 'wmDYInvoiceInfoDetail',
            url: 'wmDYInvoiceInfoDetail' + '.html',
            pageParam: {
                invoiceplancode: invoiceplancode,
                invoiceinfoid:invoiceplanid,
                subBussinessType: subBussinessType,
                titleText:"东阳计划-绑定物料",
                fhdhCode:fhdhCode
            },
        });
    }


    // 获取光标所在组件的id
    function getIdSelector() {
        var e = event || window.event;
        var el = e.srcElement || e.target;
        idSelector = el.id;
        $("#" + el.id).select();
    }

    //扫描
    function doScan() {
        var moduleScanner = api.require('moduleScanner');
        var params = {
            top: 150, //扫描窗口的上边距
            width: 250, //扫描窗口的宽
            height: 250, //扫描窗口的高
            useColor: false, //是否使用自己设定的颜色(startColor和endColor),
            //如果不使用设定的颜色就使用默认的那张网格图
            startColor: '#0F08C8FF', //渐变起始颜色
            endColor: '#7F08C8FF', //渐变结束颜色
            animTime: 2200, //扫描动画时间,单位为毫秒
        };
        moduleScanner.toScannerActivityForResult(params, function (ret, err) {

            //扫描条码
            if (idSelector == 'codeBar') {

                
            }
        });
    }


    // 确认发货
    function saveAll() {
        const submitButton = document.getElementById('submitButton');

        submitButton.style.pointerEvents = "none"

        api.showProgress({
            title: '保存中...',
        });
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/wminvoicePlanSave";
        var data = {
            values:{
                un: un,
                invoiceplancode: api.pageParam.invoiceplancode,   
            }
            
        };
        // $.ajax({
        //     type: 'post',
        //     async: false,
        //     url: url,
        //     data: data,
        //     dataType: 'json',
        //     success: function (ret) {
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function(ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                console.log(myobj.code)
                //成功
                if (myobj.code == '0') {
                    api.hideProgress();
                    submitButton.style.pointerEvents = "auto"
                    api.toast({
                        msg: "发货成功",
                        duration: 3000
                    });

                    refreshData();

                    closeWin();

                } else if (myobj.code == '1') {

                    api.hideProgress();
                    submitButton.style.pointerEvents = "auto"

                    api.toast({
                        msg: "发货失败",
                        duration: 3000
                    });

                    refreshData();
                    
                    $("#message").val(myobj.msg);
                }
            } else {
                
                api.hideProgress();
                submitButton.style.pointerEvents = "auto"
                alert("连接超时，请耐心等待");
            }
        }
        // }
        );
    }

    
    //返回按钮
    function closeWin() {
        api.closeWin({});

        api.execScript({
            name: "wmDYInvoiceInfo",
            script: 'refreshData()'
        });
    }

    //刷新数据
    function refreshData() {
        // 查询物料信息
        doSerchInvoiceInfo(invoiceplancode);

        //查询东阳发货单
        doSearchDYFHD();
    }

    // 确认发货
    function beforeSave() {
        api.confirm({
            title: '系统提示：',
            msg: '确认发货成功后无法继续绑定？\n是否确认？',
            buttons: ['确定', '取消']
        }, function (ret, err) {
            if (ret.buttonIndex == 1) {

                // 确认发货
                saveAll()
            }
        });
    }
</script>

</html>